package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.EquipmentConfig;
import com.ruoyi.baseData.domain.Team;
import com.ruoyi.baseData.domain.TeamUser;
import com.ruoyi.baseData.mapper.TeamMapper;
import com.ruoyi.baseData.service.TeamUserService;
import com.ruoyi.baseData.mapper.TeamUserMapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class TeamUserServiceImpl implements TeamUserService{

    /**
     * 根据Team获取团队
     */
    @Resource
    TeamMapper teamMapper;

    @Resource
    TeamUserMapper teamUserMapper;

    @Resource
    SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> getByTeamId(Long teamId) {
        Team team = teamMapper.selectById(teamId);
        if(null == team) {
            throw new GlobalException("该Team不存在");
        }
        //根据TeamId获取
        MPJLambdaWrapper<TeamUser> wrapper = new MPJLambdaWrapper<TeamUser>()
                .eq(TeamUser::getTeamId, teamId).ne(EquipmentConfig::getDelFlag, 1);
        List<TeamUser> teamUsers = teamUserMapper.selectList(wrapper);
        List<Long> userIds = teamUsers.stream().map(item -> item.getUserId().longValue()).distinct().collect(Collectors.toList());
        if(null == userIds || userIds.size() <= 0) {
            throw new GlobalException("用户不存在请先配置");
        }
        List<SysUser> sysUsers  = sysUserMapper.selectUserByIds(userIds);
        return sysUsers;
    }
}




